package com.ym.manager.admin.tbUser;

import com.ym.manager.admin.tbUser.model.EnumRoleName;
import com.ym.manager.admin.tbUser.model.TbUserInfo;
import com.ym.utils.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class TbUserService {

    @Autowired
    private TbUserDao tbUserDao;


    /**
     * 关系列表
     * @param pageNum
     * @param limit
     * @param mcode
     * @return
     */
    public Pages<TbUserInfo> subList(int pageNum, int limit, int mcode) {
        Map<String,Object> map = new HashMap<>();
        int begin = (pageNum-1)*limit;
        map.put("begin",begin);
        map.put("limit",limit);
        map.put("mcode",mcode);
        int count  = tbUserDao.getSubListCount(map);
        List<TbUserInfo> list = tbUserDao.subList(map);
        list = list.stream().map(x->{
            x.setRoleName(EnumRoleName.ROLE_NAME.getName(x.getRole()));
            return x;
        }).collect(Collectors.toList());
        Pages<TbUserInfo> pages = new Pages<>(count,pageNum,limit);
        pages.setList(list);
        return pages;
    }

    /**
     * 获取上级信息
     * @param mcode
     * @return
     */
    public TbUserInfo supUser(int mcode) {
        TbUserInfo userInfo = tbUserDao.supUser(mcode);
        if(userInfo!=null){
            userInfo.setRoleName(EnumRoleName.ROLE_NAME.getName(userInfo.getRole()));
        }
        return userInfo;
    }
}
